package com.maikobi.maikobi.utils;

/**
 * @author: Maiko7
 * @create: 2025-07-18 8:53
 */

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Slf4j
@Component
public class DingTalkNotifier {

    private static final MediaType JSON = MediaType.get("application/json; charset=utf-8");

    @Value("${bi.notify.dingtalk-webhook}")
    private String webhookUrl;

    private final OkHttpClient client = new OkHttpClient();

    public void sendTextMessage(String content) {
        String json = "{\n" +
                "    \"msgtype\": \"text\",\n" +
                "    \"text\": {\n" +
                "        \"content\": \"" + content + "\"\n" +
                "    }\n" +
                "}";

        RequestBody body = RequestBody.create(json, JSON);
        Request request = new Request.Builder()
                .url(webhookUrl)
                .post(body)
                .build();

        try (Response response = client.newCall(request).execute()) {
            log.info("钉钉消息发送响应: {}", response.body().string());
        } catch (IOException e) {
            log.error("钉钉通知失败", e);
        }
    }
}

